from django.db import models
from django.utils.timezone import now
from mdeditor.fields import MDTextField

from user.models import User


class Blog(models.Model):
    """
    博客信息表（个人站点表)
        注：用户和站点表是一对一的关系
    """
    uid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name='个人博客标题', max_length=64)
    site_cn_name = models.CharField(verbose_name='站点中文名称', max_length=64)
    site_en_name = models.CharField(verbose_name='站点英文名称', max_length=64)
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    objects = models.Manager()

    def __unicode__(self):
        return self.title

    class Meta:
        db_table = 'blog_info'
        verbose_name = '博客信息表'
        verbose_name_plural = verbose_name


class Category(models.Model):
    """
    文章分类表
        注：分类和博客站点是一对多
    """
    uid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name='分类标题', max_length=32)
    blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='uid', on_delete=models.CASCADE)

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'blog_category'
        verbose_name = '文章分类表'
        verbose_name_plural = verbose_name


class Tag(models.Model):
    """
    标签表
        注：一个用户可以创建多个标签，一个标签也只能属于一个站点，一个站点可以有多个标签
    """
    objects = models.Manager()
    uid = models.AutoField(primary_key=True)
    title = models.CharField(verbose_name='标签名称', max_length=32)
    blog = models.ForeignKey(verbose_name='所属博客', to='Blog', to_field='uid',
                             on_delete=models.CASCADE)  # 标签Tag跟站点Blog绑定一对多的关系

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'blog_tag'
        verbose_name = '标签表'
        verbose_name_plural = verbose_name


class Article(models.Model):
    """
    文章表
    """
    STATUS_CHOICES = (
        ('D', '草稿'),
        ('P', '发表'),
        ('D', '回收桶'),
    )
    uid = models.AutoField(primary_key=True)
    # 用户和文章 一对多
    user = models.ForeignKey(verbose_name='用户', to=User, to_field='uid', on_delete=models.CASCADE)
    title = models.CharField(verbose_name='文章标题', max_length=50)
    desc = models.CharField(verbose_name='文章描述', max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    update_time = models.DateTimeField(verbose_name='更新时间', auto_now_add=True)
    pub_time = models.DateTimeField(verbose_name='发布时间', blank=False, null=False, default=now)
    content = MDTextField()  # 修改这个类型,把content用于Markdown
    status = models.CharField(verbose_name='文章状态', max_length=1, choices=STATUS_CHOICES, default='P')
    comment_count = models.IntegerField(verbose_name='评论数', default=0)
    up_count = models.IntegerField(verbose_name='点赞数', default=0)
    collect_count = models.IntegerField(verbose_name='收藏数', default=0)

    # 分类Category和文章Article多对多
    category = models.ForeignKey(to='Category', to_field='uid', null=True,
                                 on_delete=models.CASCADE)

    # 用了一个中介模型，创建第三张表
    tags = models.ManyToManyField(
        to='Tag',
        through='Article2Tag',
        through_fields=('article', 'tag'),
    )

    def __str__(self):
        return self.title + '-' + self.user.name

    class Meta:
        db_table = 'blog_article'
        verbose_name = '博客文章表'
        verbose_name_plural = verbose_name


class Article2Tag(models.Model):
    """
    文章与标签关系表
        注：文章和标签的多对多
    """
    uid = models.AutoField(primary_key=True)
    article = models.ForeignKey(verbose_name='文章', to=Article, to_field='uid', on_delete=models.CASCADE)
    tag = models.ForeignKey(verbose_name='标签', to='Tag', to_field='uid', on_delete=models.CASCADE)

    class Meta:
        unique_together = [
            ('article', 'tag'),  # 做了个联合唯一，这两个字段不能重复
        ]
        db_table = 'blog_article_tag'
        verbose_name = '文章与标签关系表'
        verbose_name_plural = verbose_name

    def __str__(self):
        v = self.article.title + '-' + self.tag.title
        return v


class ArticleUpDown(models.Model):
    """
    点赞表
    """
    uid = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)  # 外键关联User表
    article = models.ForeignKey(Article, null=True, on_delete=models.CASCADE)
    is_up = models.BooleanField(default=True)

    class Meta:
        unique_together = [
            ('article', 'user'),
        ]

        db_table = 'blog_article_up_down'
        verbose_name = '点赞表'
        verbose_name_plural = verbose_name


class Comment(models.Model):
    """
    评论表
    """
    uid = models.AutoField(primary_key=True)
    article = models.ForeignKey(verbose_name='评论文章', to='Article', to_field='uid', on_delete=models.CASCADE)
    user = models.ForeignKey(verbose_name='评论者', to=User, to_field='uid', on_delete=models.CASCADE)
    content = models.CharField(verbose_name='评论内容', max_length=255)
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)

    parent_comment = models.ForeignKey('self', null=True, on_delete=models.CASCADE)  # 自己关联自己，自关联，可以写成self也可以写成Comment

    def __str__(self):
        return self.content

    class Meta:
        unique_together = [
            ('article', 'user'),
        ]

        db_table = 'blog_comment'
        verbose_name = '评论表'
        verbose_name_plural = verbose_name
